package dm

import (
	"log"
	"plan_go/server/util"
	"strings"
	"time"
)

func (com *DmSoft) FindWindow(class, title string) int {
	ret, _ := com.dm.CallMethod("FindWindow", class, title)
	return int(ret.Val)
}

func (com *DmSoft) GetWindow(hwnd, flag int) int {
	ret, _ := com.dm.CallMethod("GetWindow", hwnd, flag)
	return int(ret.Val)
}

func (com *DmSoft) BindWindow(hwnd int, display string, mouse string, keypad string, mode int) int {
	ret, _ := com.dm.CallMethod("BindWindow", hwnd, display, mouse, keypad, mode)
	return int(ret.Val)
}
func (com *DmSoft) BindWindowEx(hwnd int, display string, mouse string, keypad string, public string, mode int) int {
	ret, _ := com.dm.CallMethod("BindWindowEx", hwnd, display, mouse, keypad, public, mode)
	return int(ret.Val)
}

func (com *DmSoft) UnBindWindow() int {
	ret, _ := com.dm.CallMethod("UnBindWindow")
	return int(ret.Val)
}
func (com *DmSoft) ForceUnBindWindow() int {
	ret, _ := com.dm.CallMethod("ForceUnBindWindow")
	return int(ret.Val)
}

func (com *DmSoft) SetWindowState(hwnd, flag int) int {
	ret, _ := com.dm.CallMethod("SetWindowState", hwnd, flag)
	return int(ret.Val)
}

// DmGuard Type取值请查看大漠文档
func (com *DmSoft) DmGuard(enable int, types string) int {
	ret, _ := com.dm.CallMethod("DmGuard", enable, types)
	return int(ret.Val)
}

// LockInput 这个lock函数真的很重要,无论是操作前台键鼠还是后台键鼠,或是监控键盘输入,操作前必须锁一下,不然必出很多问题,谨记,谨记!!!
func (com *DmSoft) LockInput(lock int) int {
	ret, _ := com.dm.CallMethod("LockInput", lock)
	return int(ret.Val)
}

func (com *DmSoft) ExecuteCmd(cmd string, currentDir string, timeOut int) string {
	ver, _ := com.dm.CallMethod("ExecuteCmd", cmd, currentDir, timeOut)
	return ver.ToString()
}

func (com *DmSoft) EnumWindow(parent int, title string, className string, filter int) string {
	ret, _ := com.dm.CallMethod("EnumWindow", parent, title, className, filter)
	return ret.ToString()
}

func (com *DmSoft) SendString(hwnd int, str string) int {
	ret, _ := com.dm.CallMethod("SendString2", hwnd, str)
	return int(ret.Val)
}

func (com *DmSoft) SetWindowSize(hwnd, width, height int) int {
	ret, _ := com.dm.CallMethod("SetWindowSize", hwnd, width, height)
	return int(ret.Val)
}

// GetEnumWindow /**拿到目标父窗口的第一个子窗口列表*/
func (com *DmSoft) GetEnumWindow(parent int) []int {
	ret := com.EnumWindow(parent, "", "", 4)
	//将ret用，分割成整形数组
	rets := strings.Split(ret, ",")
	var reti []int
	for _, v := range rets {
		reti = append(reti, util.StrToInt(v))
	}
	return reti
}

// GetTargetWindow /**拿到目标窗口*/
func (com *DmSoft) GetTargetWindow(className string, title string, windowOrder []int) int {
	parentWindow := com.FindWindow(className, title)
	if 0 == parentWindow {
		return parentWindow
	}
	firstChildWindows := com.GetEnumWindow(parentWindow)
	log.Println("GetTargetWindow: ", title, className, windowOrder, parentWindow, firstChildWindows)
	var hwnd int
	for _, order := range windowOrder {
		hwnd = firstChildWindows[order]
		firstChildWindows = com.GetEnumWindow(hwnd)
		log.Println("firstChildWindows: ", firstChildWindows)
	}
	return hwnd
}

func (com *DmSoft) SwitchBindWindow(hwnd int) int {
	ret, _ := com.dm.CallMethod("SwitchBindWindow", hwnd)
	time.Sleep(500 * time.Microsecond)
	return int(ret.Val)
}

func (com *DmSoft) RegisterBindWindow(hwnd int, display string, mouse string, keypad string, public string, mode int) {
	com.wg.Add(1)
	go func() {
		bindRet := com.BindWindowEx(hwnd, display, mouse, keypad, public, mode)
		log.Println("hwnd,bindRet,GetLastError: ", hwnd, bindRet, com.GetLastError())
		com.wg.Wait()
	}()
}

//func (com *DmSoft)  WorkOnWindow(hwnd int, method func()) {
//	com.SetWindowState(hwnd,1)
//	time.Sleep(300*time.Millisecond)
//	if hwnd==HasBindHwnd {
//		method()
//		return
//	}
//	if HasBindHwnd != 0{
//		unBindWindowRet := com.UnBindWindow()
//		log.Println("unBindWindowRet",unBindWindowRet)
//	}
//	HasBindHwnd=hwnd
//	bindRet := com.BindWindowEx(hwnd,"normal","dx","normal","",2)
//	log.Println("hwnd,bindRet",hwnd,bindRet)
//	method()
//	com.UnBindWindow()
//}
